

package jfc.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import jfc.core.GameManager;

/**
 *
 * @author ylegall
 */
public class FileManager {

    public static boolean save(GameManager manager) throws IOException {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
        int returnCode = chooser.showSaveDialog(null);
        if(returnCode == JFileChooser.APPROVE_OPTION)
        {
            File f = chooser.getSelectedFile();
            save(manager, f);
            return true;
        }
        return false;
    }

    public static void save(GameManager manager, File f) throws IOException {
        if(f.exists()) f.delete();
        FileWriter fileWriter = new FileWriter(f);
        manager.save(fileWriter);
        fileWriter.close();
    }
}
